package com.skyolder.milkbee.service;

import com.skyolder.milkbee.domain.Leaguer;
import com.skyolder.milkbee.domain.User;
import com.skyolder.milkbee.repository.LeaguerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;

import java.time.ZonedDateTime;
import java.util.List;

/**
 * Service Implementation for managing Leaguer.
 */
@Service
@Transactional
public class LeaguerService {

    private final Logger log = LoggerFactory.getLogger(LeaguerService.class);
    
    @Inject
    private LeaguerRepository leaguerRepository;

    /**
     * Save a leaguer.
     *
     * @param leaguer the entity to save
     * @return the persisted entity
     */
    public Leaguer save(Leaguer leaguer) {
        log.debug("Request to save Leaguer : {}", leaguer);
        Leaguer result = leaguerRepository.save(leaguer);
        return result;
    }

    /**
     *  Get all the leaguers.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<Leaguer> findAll(Pageable pageable) {
        log.debug("Request to get all Leaguers");
        Page<Leaguer> result = leaguerRepository.findAll(pageable);
        return result;
    }

    /**
     *  Get one leaguer by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public Leaguer findOne(Long id) {
        log.debug("Request to get Leaguer : {}", id);
        Leaguer leaguer = leaguerRepository.findOne(id);
        return leaguer;
    }

    /**
     *  Delete the  leaguer by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Leaguer : {}", id);
        leaguerRepository.delete(id);
    }

	public Leaguer findByUser(User user) {
		// TODO Auto-generated method stub
		Leaguer leaguer= leaguerRepository.findByUserId(user.getId());
		if(leaguer==null){
			leaguer=Leaguer.newInstance();
			leaguer.setUserId(user.getId());
			leaguer.setName(user.getLogin());
			leaguer.setNikename(user.getLogin());
			leaguer.setAvatar(user.getId()+".png");
			leaguer=leaguerRepository.save(leaguer);
		}
		return leaguer;
	}

	public Page<Leaguer> doQueryFocus(Long userId, Pageable pageable) {
		// TODO Auto-generated method stub
		return leaguerRepository.doQueryFocus(userId,pageable);
	}

	public Page<Leaguer> doQueryFans(Long userId, Pageable pageable) {
		// TODO Auto-generated method stub
		return leaguerRepository.doQueryFans(userId,pageable);
	}

	public List<Leaguer> findAll() {
		return leaguerRepository.findAll();
	}

	public Page<Leaguer> doQueryByIds(List<Long> ids, Pageable pageable) {
		// TODO Auto-generated method stub
		return leaguerRepository.doQueryByIds(ids,pageable);
	}

	public List<Leaguer> findByMonitorTrue() {
		// TODO Auto-generated method stub
		return leaguerRepository.findByMonitorTrue();
	}

	public Leaguer findOneByUserId(Long userId) {
		return leaguerRepository.findOneByUserId(userId);
	}

	public Page<Leaguer> queryApply(Pageable pageable) {
		return leaguerRepository.queryApply(pageable);
	}

	public Page<Leaguer> queryNoReal(Pageable pageable) {
		return leaguerRepository.queryNoReal(pageable);
	}
	public Page<Leaguer> queryReal(Pageable pageable) {
		return leaguerRepository.queryReal(pageable);
	}

	public Page<Leaguer> queryForbidden(Pageable pageable) {
		return leaguerRepository.queryForbidden(pageable);
	}

	public Page<Leaguer> queryReport(Pageable pageable) {
		return leaguerRepository.queryReport(pageable);
	}
}
